// vuex的状态机,根据功能模块划分为设备管理，设备点检，设备维修，设备保养等
import {
    fetchList,
    startUserInfo,
    stopUserInfo,
    fetchBroadInfo,
    saveInfo,
    saveDeviceInfo,
    fetchDetailInfo,
    exportFile,
    downloadTemplete,
    deleteDeviceInfo,
    sortDepartmentInfo
} from "@/api/system-fields/index";
import { reduceNullContent, deepClone } from "@/utils/index";
import data from "@/utils/constData";
const state = {
    listSearchParams: {},
    paginationInfo: {},
    records: [], //表格数据
    pages: 1,
    total: 20,
    mesg: "",
    /**
     * 编辑相关
     */
    broadInfo: [], //动态加载后端配置的扩展信息
    detailInfo: {}, //详情模板
    totalForm: {}
};
const formModel = {};
const mutations = {
    SET_LISTSERACH_PARAMS: (state, args) => {
        state.listSearchParams = args;
    },
    SET_LISTDATA: (state, args) => {
        state.records = data.concat(args);
    },
    SET_ONELISTDATA: (state, args) => {
        state.records = state.records.concat(args);
    },
    DELONE_LISTDATA: (state, args) => {
        //删除逻辑在action处理
        state.records = args;
    },
    MOVE_LISTDATA: (state, args) => {
        state.records = args;
    },
    SET_BROAD_INFO: (state, args) => {
        state.totalForm[1].formComponent = args;
        state.totalForm[1].formModel = formModel;
    },
    SET_TOTAL_FORM: (state, args) => {
        state.totalForm = args;
    },
    SET_DETAIL_INFO: (state, args) => {
        state.detailInfo = args;
    },
    SET_PAGINATION: (state, args) => {
        state.paginationInfo = args;
    }
};

const actions = {
    // 设备台账查询参数
    setListSearchParams({ commit, state }, args) {
        const data = Object.assign({}, state.listSearchParams, args);
        commit("SET_LISTSERACH_PARAMS", data);
    },
    //列表
    requestFieldList({ commit, state }, args) {
        return fetchList(reduceNullContent(args)).then(response => {
            if (response.data) {
                commit("SET_LISTDATA", response.data);
            } else {
                commit("SET_LISTDATA", []);
            }
        });
    },
    setTotalForm({ commit, state }, args) {
        console.log(args, "total");
        commit("SET_TOTAL_FORM", args);
    },
    //保存设备台账
    saveDeviceInfo({ commit, state }, args) {
        return saveDeviceInfo(args);
    },
    //保存配置
    saveInfo({ commit, state }, args) {
        const data = state.records.slice(12); //前12项无用数据要过滤
        const a = data.map((e, i) => {
            e.fieldSort = i;
            return e;
        });
        return saveInfo(a);
    },
    //新增一条数据
    addOneListData({ commit, state }, args) {
        commit("SET_ONELISTDATA", [args]);
    },
    //部门排序
    sortDepartmentInfo({ commit, state }, args) {
        return sortDepartmentInfo(args);
    },
    //设备台账单条数据的扩展信息模板请求
    requestDeviceBroadInfo({ commit, state }, args) {
        return fetchBroadInfo().then(response => {
            const array = response.data.map(e => {
                formModel[e.fieldCode] = "";
                e.type = e.fieldType == 1 ? "input" : "select";
                e.label = e.name || "";
                //e.model = e.fieldCode
                e.rules = [{ required: true, message: e.description, trigger: "blur" }];
                return e;
            });
            console.log("设备台账扩展信息", array);
            commit("SET_BROAD_INFO", array);

            // Just to simulate the time of the request
        });
    },
    //设置设备台账对象
    setDetailInfo({ commit, state }, args) {
        commit("SET_DETAIL_INFO", args);
    },
    //启用
    useUserInfo({ commit, state }, args) {
        return startUserInfo(args);
    },
    //停用
    uselessUserInfo({ commit, state }, args) {
        return stopUserInfo(args);
    },
    //请求台账详情
    requestDeviceDetailInfo({ commit, state }, args) {
        return fetchDetailInfo(args).then(response => {
            console.log("设备台账详细信息", response);
            const data = {};
            data.detailInfo = state.detailInfo.detailInfo.map(item => {
                item.value = response["data"][item.name];
                return item;
            });
            data.broadInfo = state.detailInfo.broadInfo.map(item => {
                item.value = JSON.parse(response["data"].extra)[item.name];
                return item;
            });
            // data.histroyShowInfo=state.detailInfo.histroyShowInfo.map(item=>{
            //     item.value=state.detailInfo.detailInfo.archives
            //     return item
            // })
            commit("SET_DETAIL_INFO", data);

            // Just to simulate the time of the request
        });
    },
    //导出
    exportFile({ commit, state }, args) {
        return exportFile();
    },
    //下载批量导入模板
    downloadTemplete({ commit, state }, args) {
        const data = {};
        data.objectName = "deviceTemplate.xls";
        data.expires = 3000;
        return downloadTemplete(data);
    },
    //删除设备台账
    deleteDeviceInfo({ commit, state }, args) {
        return deleteDeviceInfo(args.id).then(res => {
            console.log(res, "++++res==s=");
            const data = state.records.filter(e => {
                return e.id != args.id || e.notFromInterface;
            });
            commit("DELONE_LISTDATA", data);
        });
    },

    //移动设备台账
    moveDeviceInfo({ commit, state }, args) {
        console.log(args, "+++mov argd");
        commit("MOVE_LISTDATA", args);
    },

    //设置分页器
    setPagination({ commit, state }, args) {
        const data = {};
        data.sizeChange = args.sizeChange;
        data.currentChange = args.currentChange;
        data.current = state.current;
        data.total = state.total;
        commit("SET_PAGINATION", data);
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};